import {uuid} from '@native/utils/util';
import {WebView} from '@native/core/webview/webview';

export class Bridge{
    constructor(opts){
        this.id=`bridge_${uuid()}`;
        this.opts=opts;
        this.webview=null;
        this.jscore=opts.jscore;
        this.parent=null;
        this.status=0;
        this.jscore.addEventListener('message',this.jscoreMessageHandler.bind(this));
        

    }
    jscoreMessageHandler(msg){
        const {type,body}=msg;
        if(body.bridgeId!=this.id){
            return false;
        }
        switch(type){
            case 'logicResourceLoaded':
                this.status++;
                this.createApp();
                break;
            case 'appIsCreated':
                this.status++;
                this.notifyMakeInitialData();
                break;
            case 'initialDataIsReady':
                this.status++;
                this.setInitialData(msg);
                break;
            default:
                break;
        }
    }
    uiMessageHandler(msg){
        const {type}=msg;
        switch(type){
            case 'uiResourceLoaded':
                this.status++;
                this.createApp();
                break;
            case 'moduleCreated':
                this.uiInstanceCreated(msg.body);
                break;
            case 'moduleMounted':
                this.uiInstanceMounted(msg.body);
                break;
            case 'pageScroll':
                this.pageScroll(msg.body);
                break;
            default:
                break;
        }
    }
    pageScroll(msg){
        const {id,scrollTop}=msg;
        this.jscore.postMessage({
            type:'pageScroll',
            body:{
                id,
                scrollTop
            }
        })
    }
    notifyMakeInitialData(){

        this.jscore.postMessage({
            type:'makePageInitialData',
            body:{
                bridgeId:this.id,
                pagePath:this.opts.pagePath,
            }
        })
    }
    appShow(){
        if(this.status<2){
            return false;
        }
        this.jscore.postMessage({
            type:'appShow',
            body:{}
        })
    }
    pageShow(){
        if(this.status<2){
            return false;
        }
        this.jscore.postMessage({
            type:'pageShow',
            body:{
                bridgeId:this.id,
            }
        })
    }
    appHide(){
        if(this.status<2){
            return false;
        }
        this.jscore.postMessage({
            type:'appHide',
            body:{}
        })
    }
    pageHide(){
        if(this.status<2){
            return false;
        }
        this.jscore.postMessage({
            type:'pageHide',
            body:{
                bridgeId:this.id,
            }
        })
    }
    setInitialData(){
        const { initialData } = msg.body;
        this.webview.postMessage({
            type:'setInitialData',
            body:{
                initialData,
                bridgeId:this.id,
                pagePath:this.opts.pagePath,
            }
        })
    }
    uiInstanceCreated(msg){
        const {id,path}=msg;
        this.jscore.postMessage({
            type:'createInstance',
            body:{
                id,
                path,
                bridgeId:this.id,
                query:this.opts.query,
            }
        })
    }
    uiInstanceMounted(msg){
        const {id}=msg;
        this.jscore.postMessage({
            type:'moduleMounted',
            body:{
                id,
            }
        })
    }
    createApp(){
        if(this.status==2){
            return false;
        }

        this.jscore.postMessage({
            type:'createApp',
            body:{
                bridgeId:this.id,
                scene:this.opts.scene,
                pagePath:this.opts.pagePath,
                query:this.opts.query
            }
        })
    }

    appShow(){
        if(this.status<2){
            return false;
        }

        this.jscore.postMessage({
            type:'appShow',
            body:{

            }
        })
    }
    appHide(){
        if(this.status<2){
            return false;
        }

        this.jscore.postMessage({
            type:'appHide',
            body:{

            }
        })
    }
    start(){
        this.webview.postMessage({
            type:'loadResource',
            body:{
                appId:this.opts.appId,
            }
        })
        this.jscore.postMessage({
            type:'loadResource',
            body:{
                appId:this.opts.appId,
                bridgeId:this.id,
            }
        })
    }

    async init(){
        this.webview=await this.createWebview();
        this.webview.addEventListener('message',this.uiMessageHandler.bind(this));
    }

    createWebview(){
        return new Promise((resolve,reject)=>{
            const webview=new WebView({
                configInfo:this.opts.configInfo,
                isRoot:this.opts.isRoot,
            })
            webview.parent=this;
            webview.init(()=>{
                resolve(webview);
            });
            this.parent.webviewsContainer.appendChild(webview.el);
        })
    }
}